﻿using AutoMapper;
using Nest;

namespace Journeo_backend.Services.Implementations
{
    public class RegionService : IRegionService
    {
        private readonly IMapper _mapper;
        private readonly ApplicationDbContext _context;
        public RegionService(IMapper mapper, ApplicationDbContext context)
        {
            _mapper = mapper;
            _context = context;
        }
        // 通过地区ID获取地区详情
        public async Task<Response<Region>> GetRegionDetailById(long regionId)
        {
            try
            {
                Region region = await _context.Region.FindAsync(regionId);
                return new Response<Region>(region);
            }
            catch (Exception e)
            {
                return new Response<Region>("error", e.Message);
            }
        }

        // 通过父地区ID获取地区列表
        public async Task<PageResponse<Region>> GetRegionsByParentId(int pageNumber, int pageSize, long parentId)
        {
            try
            {
                //根据父地区Id进行搜索
                var searchResponse = _context.Region.Where(r => r.ParentId == parentId).ToList();
                //根据pagenumber和pagesize划分返回数据
                var regions = searchResponse.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                return new PageResponse<Region>(regions, pageNumber, pageSize, searchResponse.Count);
            }
            catch (Exception e)
            {
                return await Task.FromResult(new PageResponse<Region>("error", e.Message, null, pageNumber, pageSize, 0));
            }
        }
    }
}
